package demo.hdz.mediacodecdecode;

import android.util.Log;

import java.io.File;
import java.io.RandomAccessFile;

/**
 *
 * @author hdz
 * @date 2018/1/20
 */

public class Logger {

    private static final String TAG = "MediaCodecDecode";
    private static final boolean mIsSave2File = false;
    private static final boolean mIsOpenLog   = true;


    private static void log(int level, String tag, String log) {
        String sLog = log;
/*
        try {
            StackTraceElement[] a = new Throwable().getStackTrace();
            StackTraceElement ste = a[0];
            for (StackTraceElement anA : a) {
                if (!"d".equals(anA.getMethodName())
                        && !"w".equals(anA.getMethodName())
                        && !"v".equals(anA.getMethodName())
                        && !"e".equals(anA.getMethodName())
                        && !"log".equals(anA.getMethodName())
                        && !"i".equals(anA.getMethodName())) {
                    ste = anA;
                    break;
                }
            }
            String  sClassName = ste.getClassName();
            String sMethodName = ste.getMethodName();
            int          iLine = ste.getLineNumber();
            String   sFileName = ste.getFileName();

            String sTmpClassName = "";
            int iPos = sClassName.lastIndexOf(".");
            if(iPos <=0 ) {
                sTmpClassName = sClassName;
            } else {
                sTmpClassName = sClassName.substring(iPos+1);
            }
            sLog = "[" + sFileName + ":" + iLine + " " + sTmpClassName + "::" + sMethodName + "] " + log;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
*/
        if (mIsOpenLog) {
            switch (level) {
                case Log.DEBUG:
                    Log.d(tag, sLog);
                    break;
                case Log.INFO:
                    Log.i(tag, sLog);
                    break;
                case Log.WARN:
                    Log.w(tag, sLog);
                    break;
                case Log.ERROR:
                    Log.e(tag, sLog);
                    break;
                case Log.VERBOSE:
                    Log.v(tag, sLog);
                default:
                    break;
            }
        }
        if (mIsSave2File) {
            WriteTxtFile(sLog.getBytes(), "/sdcard/log.txt");
        }
    }

    public static void v(String tag, String log) {Logger.log(Log.VERBOSE, tag, log);}
    public static void v(String tag, int log) {
        v(tag, "" + log);
    }
    public static void v(String tag, String log, boolean out) {if (out){v(tag, log);}}
    public static void v(String tag, int    log, boolean out) {if (out){v(tag, log);}}
    public static void v(String log, boolean out) {if (out){v(log);}}
    public static void v(int    log, boolean out) {if (out){v(log);}}
    public static void v(String log) {v(TAG, log);}
    public static void v(int log) {v(TAG, ""+log);}

    public static void d(String tag, String log) {Logger.log(Log.DEBUG, tag, log);}
    public static void d(String tag, int log) {
        d(tag, "" + log);
    }
    public static void d(String tag, String log, boolean out) {if (out){d(tag, log);}}
    public static void d(String tag, int    log, boolean out) {if (out){d(tag, log);}}
    public static void d(String log, boolean out) {if (out){d(log);}}
    public static void d(int    log, boolean out) {if (out){d(log);}}
    public static void d(String log) {d(TAG, log);}
    public static void d(int log) {d(TAG, ""+log);}


    public static void i(String tag, String log) {Logger.log(Log.INFO, tag, log);}
    public static void i(String tag, int log) {i(tag, "" + log);}
    public static void i(String tag, String log, boolean out) {if (out){i(tag, log);}}
    public static void i(String tag, int    log, boolean out) {if (out){i(tag, log);}}
    public static void i(String log, boolean out) {if (out){i(log);}}
    public static void i(int    log, boolean out) {if (out){i(log);}}
    public static void i(String log) {i(TAG, log);}
    public static void i(int log) {i(TAG, ""+log);}


    public static void w(String tag, String log) {Logger.log(Log.WARN, tag, log);}
    public static void w(String tag, int log) {
        w(tag, "" + log);
    }
    public static void w(String tag, String log, boolean out) {if (out){w(tag, log);}}
    public static void w(String tag, int    log, boolean out) {if (out){w(tag, log);}}
    public static void w(String log, boolean out) {if (out){w(log);}}
    public static void w(int    log, boolean out) {if (out){w(log);}}
    public static void w(String log) {w(TAG, log);}
    public static void w(int log) {w(TAG, ""+log);}


    public static void e(String tag, String log) {Logger.log(Log.ERROR, tag, log);}
    public static void e(String tag, int log) {e(tag, "" + log);}
    public static void e(String tag, String log, boolean out){if (out){e(tag, log);}}
    public static void e(String tag, int    log, boolean out){if (out){e(tag, log);}}
    public static void e(String log, boolean out){if (out){ e(log);}}
    public static void e(int    log, boolean out){if (out){ e(log);}}
    public static void e(String log) {e(TAG, log);}
    public static void e(int    log) {e(TAG, ""+log);}


    public static void WriteTxtFile(byte[] b, String strFilePath) {
        try {
            File file = new File(strFilePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.seek(file.length());
            raf.write(b);
            raf.write("\n".getBytes());
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
